home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / uwin / cassette / cassedoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  14.5 KB  |  531 lines

  1. // Copyright (c) 1994, William Wagner
  2. // All Rights reserved.
  3. //
  4. // This source is a portion of a shareware program.  It may be distributed
  5. // only in its entirety.  The copyright statements must be included with any 
  6. // reproduction of this source.
  7. // 
  8.  
  9. // cassedoc.cpp : implementation of the CCassetteDoc class
  10. //
  11.  
  12. #include "stdafx.h"
  13. #include "cassette.h"
  14.  
  15. #include "cassedoc.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. //Version number for cassette documents. 
  23. //This way, future versions can read/convert old versions.
  24. static const LONG DOC_VERSION  = 100;
  25. //This application uses MM_LOENGLISH, which is 100 pixels
  26. // per inch.
  27. static const short PIXPERINCH = 100; 
  28.  
  29. //There are 72 points per inch.
  30. static const short POINTSPERINCH = 72;
  31.  
  32. //Little constant to make debug output easier:
  33. #ifdef _DEBUG
  34. static const CString eol ="\n";
  35. #endif
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CCassetteDoc
  39.  
  40. IMPLEMENT_DYNCREATE(CCassetteDoc, CDocument)
  41.  
  42. BEGIN_MESSAGE_MAP(CCassetteDoc, CDocument)
  43.     //{{AFX_MSG_MAP(CCassetteDoc)
  44.     ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
  45.     ON_COMMAND(ID_FONTS_ALBUM, OnFontsAlbum)
  46.     ON_COMMAND(ID_FONTS_ARTIST, OnFontsArtist)
  47.     ON_COMMAND(ID_FONTS_NOTES, OnFontsNotes)
  48.     ON_COMMAND(ID_FONTS_SONGS, OnFontsSongs)
  49.     //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CCassetteDoc construction/destruction
  54.  
  55. //Initialize all the members to known states.  
  56. //The CStrings are initialized using the default constructors.
  57. //InitFont initializes all the fonts.
  58. //
  59. //PreConditions:  None.
  60. //PostConditions:  A valid object.
  61. CCassetteDoc::CCassetteDoc() :
  62.     m_lVersion (DOC_VERSION)
  63. {
  64. InitFont (&m_lfontSongs);
  65. InitFont (&m_lfontArtist);
  66. InitFont (&m_lfontAlbum);
  67. InitFont (&m_lfontNotes);
  68.  
  69. //PostConditions:
  70. ASSERT_VALID (this);
  71. }
  72.  
  73. //Destructor:  Does nothing.
  74. CCassetteDoc::~CCassetteDoc()
  75. {
  76. }
  77.  
  78. //This initializes the logical font structure passed in to a 
  79. //MS Sans Serif 8 point font.  The other constants used 
  80. //define the mapping mode and the size in inches.
  81. //
  82. //PreConditions: Font is non-null.
  83. //PostConditions: A valid logical font. (not enforced).
  84. void CCassetteDoc::InitFont (LOGFONT* Font)
  85. {
  86. //PreConditions:
  87. //This is called during construction, so don't assume a 
  88. //valid object.
  89. ASSERT (NULL != Font);
  90.  
  91. Font->lfHeight = (-(PIXPERINCH * 8 / POINTSPERINCH));
  92. Font->lfWidth  = 0;
  93. Font->lfEscapement  = 0;
  94. Font->lfOrientation = 0;
  95. Font->lfWeight = 0;
  96. Font->lfItalic = 0;
  97. Font->lfUnderline = 0;
  98. Font->lfStrikeOut = 0;
  99. Font->lfCharSet    = ANSI_CHARSET;
  100. Font->lfOutPrecision = OUT_DEFAULT_PRECIS;
  101. Font->lfClipPrecision = CLIP_DEFAULT_PRECIS;
  102. Font->lfQuality = DEFAULT_QUALITY;
  103. Font->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  104. strncpy(Font->lfFaceName, "MS Sans Serif", LF_FACESIZE);
  105. //PostConditions:
  106. //Not enforced.
  107. }
  108.  
  109. //Clear this document for reuse.  
  110. //This sets all the string values to NULL strings.
  111. //The version is set to the current version.  The 
  112. //fonts are not reset.  They are preserved from tape to tape.
  113. //PreConditions: A valid object.
  114. //PostConditions: A valid empty object (not enforced)
  115. void CCassetteDoc::DeleteContents ()
  116. {
  117. //PreConditions:
  118. ASSERT_VALID (this);
  119.  
  120. m_lVersion = DOC_VERSION;
  121. m_csAlbum1.Empty ();
  122. m_csAlbum2.Empty ();
  123. m_csArtist1.Empty ();
  124. m_csArtist2.Empty ();
  125. m_csSongs1.Empty ();
  126. m_csSongs2.Empty ();
  127. m_csNotes.Empty ();
  128. //PostConditions:
  129. ASSERT_VALID (this);
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CCassetteDoc serialization
  134.  
  135. //This utility function reads the information in the 
  136. // font parameter from the archive object specified by ar.
  137. //The casts to longs are to get the default serialization. 
  138. // ints do not have serialize functions, because their size may change.
  139. //
  140. //Preconditions: A valid object, the archive is loading, Font is non-Null.
  141. //PostConditions: Read everything.  (Enforced by checking the validity of this.
  142. void CCassetteDoc::ReadFont (CArchive& ar, LOGFONT* font)
  143. {
  144. ASSERT_VALID (this);
  145. ASSERT (ar.IsLoading ());
  146. ASSERT (NULL != font);
  147.  
  148. CString FaceName;
  149. ar >> (LONG&)font->lfHeight;
  150. ar >> (LONG&)font->lfWidth;
  151. ar >> (LONG&)font->lfEscapement;
  152. ar >> (LONG&)font->lfOrientation;
  153. ar >> (LONG&)font->lfWeight;
  154. ar >> font->lfItalic;
  155. ar >> font->lfUnderline;
  156. ar >> font->lfStrikeOut;
  157. ar >> font->lfCharSet;
  158. ar >> font->lfOutPrecision;
  159. ar >> font->lfClipPrecision;
  160. ar >> font->lfQuality;
  161. ar >> font->lfPitchAndFamily;
  162. ar >> FaceName;
  163. strncpy (font->lfFaceName, FaceName, LF_FACESIZE);
  164.  
  165. ASSERT_VALID (this);
  166. }
  167.  
  168. //This utility function writes the information in the 
  169. // font parameter to the archive object specified by ar.
  170. //The casts to longs are to get the default serialization. 
  171. // ints do not have serialize functions, because their size may change.
  172. //
  173. //PreConditions:  Valid object; archive is storing, font is non-null.
  174. //PostConditions: Still a valid object.
  175. void CCassetteDoc::WriteFont (CArchive& ar, LOGFONT* font)
  176. {
  177. //PreConditions:
  178. ASSERT_VALID (this);
  179. ASSERT (ar.IsStoring ());
  180. ASSERT (NULL != font);
  181.  
  182. ar << (LONG) font->lfHeight;
  183. ar << (LONG) font->lfWidth;
  184. ar << (LONG) font->lfEscapement;
  185. ar << (LONG) font->lfOrientation;
  186. ar << (LONG) font->lfWeight;
  187. ar << font->lfItalic;
  188. ar << font->lfUnderline;
  189. ar << font->lfStrikeOut;
  190. ar << font->lfCharSet;
  191. ar << font->lfOutPrecision;
  192. ar << font->lfClipPrecision;
  193. ar << font->lfQuality;
  194. ar << font->lfPitchAndFamily;
  195. ar << font->lfFaceName;
  196.  
  197. //PostConditions:
  198. ASSERT_VALID (this);
  199. }
  200.  
  201. //This private function reads a document from an archive 
  202. // object.  It serializes the simple types and then 
  203. // calls other private utility functions to finish
  204. // the job.
  205. //
  206. //PreConditions: a valid object, as is Loading.
  207. //PostConditions: a valid object.
  208. void CCassetteDoc::ReadDocument (CArchive& ar)
  209. {
  210. //PreConditions:
  211. ASSERT_VALID (this);
  212. ASSERT (ar.IsLoading ());
  213.  
  214. ar >> m_lVersion;
  215. ar >> m_csAlbum1;
  216. ar >> m_csAlbum2;
  217. ar >> m_csArtist1;
  218. ar >> m_csArtist2;
  219. ar >> m_csSongs1;
  220. ar >> m_csSongs2;
  221. ar >> m_csNotes;
  222. ReadFont (ar, &m_lfontSongs);
  223. ReadFont (ar, &m_lfontArtist);
  224. ReadFont (ar, &m_lfontAlbum);
  225. ReadFont (ar, &m_lfontNotes);
  226.  
  227. //PostConditions:
  228. ASSERT_VALID (this);
  229. }
  230.  
  231. //This private function writes a document to the archive file.
  232. // It seraializes simple objects and calls other functions to
  233. // write the more complex font information.
  234. //
  235. //PreConditions:  a valid object, archive is storing.
  236. //PostConditions: a valid object.
  237. void CCassetteDoc::WriteDocument (CArchive& ar)
  238. {
  239. //PreConditions:
  240. ASSERT_VALID (this);
  241. ASSERT (ar.IsStoring ());
  242.  
  243. m_lVersion = DOC_VERSION;
  244. ar << m_lVersion;
  245. ar << m_csAlbum1;
  246. ar << m_csAlbum2;
  247. ar << m_csArtist1;
  248. ar << m_csArtist2;
  249. ar << m_csSongs1;
  250. ar << m_csSongs2;
  251. ar << m_csNotes;
  252. WriteFont (ar, &m_lfontSongs);
  253. WriteFont (ar, &m_lfontArtist);
  254. WriteFont (ar, &m_lfontAlbum);
  255. WriteFont (ar, &m_lfontNotes);
  256.  
  257. //PostConditions:
  258. ASSERT_VALID (this);
  259. }
  260.  
  261. //This is the serialize function for the cassette label
  262. //document.
  263. //
  264. //PreConditions: this is valid, ar is valid.
  265. //PostConditions: same.
  266. void CCassetteDoc::Serialize(CArchive& ar)
  267. {
  268. //PreConditions:
  269. ASSERT_VALID (this);
  270.  
  271. if (ar.IsStoring())
  272.     WriteDocument (ar);
  273. else
  274.     ReadDocument (ar);
  275.  
  276. ASSERT_VALID (this);
  277. }
  278.  
  279. //This function changes a single font.
  280. //The work is done by the CFontDialog.
  281. //
  282. //PreConditions: A valid object, valid font.
  283. //PostConditions: still a valid object.  The return value
  284. // is one of the expected values.
  285. int CCassetteDoc::ChangeFont (LOGFONT* Font)
  286. {
  287. //PreConditions:
  288. ASSERT_VALID (this);
  289. ASSERT (NULL != Font);
  290.  
  291. int retval;
  292. CFontDialog Fontdlg (Font,CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT);
  293. ASSERT_VALID (&Fontdlg);
  294.  
  295. retval = Fontdlg.DoModal ();
  296.  
  297. //PostConditions:
  298. ASSERT_VALID (this);
  299. ASSERT (retval != 0);
  300. ASSERT (IDOK == retval || IDCANCEL == retval);
  301.  
  302. return retval;
  303. }
  304.  
  305.  
  306. /////////////////////////////////////////////////////////////////////////////
  307. // CCassetteDoc diagnostics
  308.  
  309. #ifdef _DEBUG
  310. void CCassetteDoc::AssertValid() const
  311. {
  312. CDocument::AssertValid();
  313.  
  314. ASSERT (DOC_VERSION == m_lVersion);
  315. //Can't ASSERT_VALID on CStrings, or LOGFONTS
  316. // So here, we assert the length of the strings.
  317. ASSERT (m_csAlbum1.GetLength () <= 256);
  318. ASSERT (m_csAlbum2.GetLength () <= 256);
  319. ASSERT (m_csArtist1.GetLength () <= 128);
  320. ASSERT (m_csArtist2.GetLength () <= 128);
  321. ASSERT (m_csSongs1.GetLength () <= 1024);
  322. ASSERT (m_csSongs2.GetLength () <= 1024);
  323. ASSERT (m_csNotes.GetLength () <= 1024);
  324. //Skip the LogFonts.  It is not clear exactly what 
  325. // makes a valid LOGFONT.  
  326. }
  327.  
  328. //Dump.
  329. // Print all the members of this object.  This
  330. // definitely breaks my small function rule, but it
  331. // is just a a debug member.  It makes no difference
  332. // to the performance.
  333. void CCassetteDoc::Dump(CDumpContext& dc) const
  334. {
  335. CDocument::Dump(dc);
  336. dc << "m_lVersion: ";
  337. dc << m_lVersion << eol;
  338. dc << "m_csAlbum1: ";
  339. dc << m_csAlbum1 << eol;
  340. dc << "m_csAlbum2: ";
  341. dc << m_csAlbum2 << eol;
  342. dc << "m_csArtist1: ";
  343. dc << m_csArtist1 << eol;
  344. dc << "m_csArtist2: ";
  345. dc << m_csArtist2 << eol;
  346. dc << "m_csSongs1: ";
  347. dc << m_csSongs1 << eol;
  348. dc << "m_csSongs2: ";
  349. dc << m_csSongs2 << eol;
  350. dc << "m_csNotes: ";
  351. dc << m_csNotes << eol;
  352.  
  353. dc << "m_lfontSongs: " << eol;
  354. dc << (LONG) m_lfontSongs.lfHeight << eol;
  355. dc << (LONG) m_lfontSongs.lfWidth << eol;
  356. dc << (LONG) m_lfontSongs.lfEscapement << eol;
  357. dc << (LONG) m_lfontSongs.lfOrientation << eol;
  358. dc << (LONG) m_lfontSongs.lfWeight << eol;
  359. dc << m_lfontSongs.lfItalic << eol;
  360. dc << m_lfontSongs.lfUnderline << eol;
  361. dc << m_lfontSongs.lfStrikeOut << eol;
  362. dc << m_lfontSongs.lfCharSet << eol;
  363. dc << m_lfontSongs.lfOutPrecision << eol;
  364. dc << m_lfontSongs.lfClipPrecision << eol;
  365. dc << m_lfontSongs.lfQuality << eol;
  366. dc << m_lfontSongs.lfPitchAndFamily << eol;
  367. dc << m_lfontSongs.lfFaceName << eol;
  368.  
  369. dc << "m_lfontArtist: " << eol;
  370. dc << (LONG) m_lfontArtist.lfHeight << eol;
  371. dc << (LONG) m_lfontArtist.lfWidth << eol;
  372. dc << (LONG) m_lfontArtist.lfEscapement << eol;
  373. dc << (LONG) m_lfontArtist.lfOrientation << eol;
  374. dc << (LONG) m_lfontArtist.lfWeight << eol;
  375. dc << m_lfontArtist.lfItalic << eol;
  376. dc << m_lfontArtist.lfUnderline << eol;
  377. dc << m_lfontArtist.lfStrikeOut << eol;
  378. dc << m_lfontArtist.lfCharSet << eol;
  379. dc << m_lfontArtist.lfOutPrecision << eol;
  380. dc << m_lfontArtist.lfClipPrecision << eol;
  381. dc << m_lfontArtist.lfQuality << eol;
  382. dc << m_lfontArtist.lfPitchAndFamily << eol;
  383. dc << m_lfontArtist.lfFaceName << eol;
  384.  
  385. dc << "m_lfontAlbum: ";
  386. dc << (LONG) m_lfontAlbum.lfHeight << eol;
  387. dc << (LONG) m_lfontAlbum.lfWidth << eol;
  388. dc << (LONG) m_lfontAlbum.lfEscapement << eol;
  389. dc << (LONG) m_lfontAlbum.lfOrientation << eol;
  390. dc << (LONG) m_lfontAlbum.lfWeight << eol;
  391. dc << m_lfontAlbum.lfItalic << eol;
  392. dc << m_lfontAlbum.lfUnderline << eol;
  393. dc << m_lfontAlbum.lfStrikeOut << eol;
  394. dc << m_lfontAlbum.lfCharSet << eol;
  395. dc << m_lfontAlbum.lfOutPrecision << eol;
  396. dc << m_lfontAlbum.lfClipPrecision << eol;
  397. dc << m_lfontAlbum.lfQuality << eol;
  398. dc << m_lfontAlbum.lfPitchAndFamily << eol;
  399. dc << m_lfontAlbum.lfFaceName << eol;
  400.  
  401. dc << "m_lfontNotes: ";
  402. dc << (LONG) m_lfontNotes.lfHeight << eol;
  403. dc << (LONG) m_lfontNotes.lfWidth << eol;
  404. dc << (LONG) m_lfontNotes.lfEscapement << eol;
  405. dc << (LONG) m_lfontNotes.lfOrientation << eol;
  406. dc << (LONG) m_lfontNotes.lfWeight << eol;
  407. dc << m_lfontNotes.lfItalic << eol;
  408. dc << m_lfontNotes.lfUnderline << eol;
  409. dc << m_lfontNotes.lfStrikeOut << eol;
  410. dc << m_lfontNotes.lfCharSet << eol;
  411. dc << m_lfontNotes.lfOutPrecision << eol;
  412. dc << m_lfontNotes.lfClipPrecision << eol;
  413. dc << m_lfontNotes.lfQuality << eol;
  414. dc << m_lfontNotes.lfPitchAndFamily << eol;
  415. dc << m_lfontNotes.lfFaceName << eol;
  416. }
  417. #endif //_DEBUG
  418.  
  419. /////////////////////////////////////////////////////////////////////////////
  420. // CCassetteDoc commands
  421.  
  422. //Update the file save command.  The command is enabled if the 
  423. // document has been modified.
  424. //PreConditions: valid object, valid parms.
  425. //PostCondition: same.
  426. void CCassetteDoc::OnUpdateFileSave(CCmdUI* pCmdUI)
  427. {
  428. //PreConditions:
  429. ASSERT_VALID (this);
  430. ASSERT (ID_FILE_SAVE == pCmdUI->m_nID);
  431.  
  432. pCmdUI->Enable (IsModified ());    
  433.  
  434. //PostConditions:
  435. ASSERT_VALID (this);
  436. }
  437.  
  438. //Handle the fonts:album command.
  439. // Call ChangeFont with the album font.
  440. // Check the return to determine if the views need to be updated.
  441. //PreConditions: valid object.
  442. //PostConditions: same.
  443. void CCassetteDoc::OnFontsAlbum()
  444. {
  445. ASSERT_VALID (this);
  446. if (IDOK == ChangeFont (&m_lfontAlbum))
  447.     {
  448.     SetModifiedFlag (TRUE);
  449.     UpdateAllViews (NULL, FONT_ALBUM_CHANGE, NULL);
  450.     }
  451. ASSERT_VALID (this);
  452. }
  453.  
  454. //Handle the fonts:artist command.
  455. // Call ChangeFont with the artist font.
  456. // Check the return to determine if the views need to be updated.
  457. //PreConditions: valid object.
  458. //PostConditions: same.
  459. void CCassetteDoc::OnFontsArtist()
  460. {
  461. ASSERT_VALID (this);
  462. if (IDOK == ChangeFont (&m_lfontArtist))
  463.     {
  464.     SetModifiedFlag (TRUE);
  465.     UpdateAllViews (NULL, FONT_ARTIST_CHANGE, NULL);
  466.     }
  467. ASSERT_VALID (this);
  468. }
  469.  
  470. //Handle the fonts:notes command.
  471. // Call ChangeFont with the notes font.
  472. // Check the return to determine if the views need to be updated.
  473. //PreConditions: valid object.
  474. //PostConditions: same.
  475. void CCassetteDoc::OnFontsNotes()
  476. {
  477. ASSERT_VALID (this);
  478. if (IDOK == ChangeFont (&m_lfontNotes))
  479.     {
  480.     SetModifiedFlag (TRUE);
  481.     UpdateAllViews (NULL, FONT_NOTES_CHANGE, NULL);
  482.     }
  483. ASSERT_VALID (this);
  484. }
  485.  
  486. //Handle the fonts:songs command.
  487. // Call ChangeFont with the songs font.
  488. // Check the return to determine if the views need to be updated.
  489. //PreConditions: valid object.
  490. //PostConditions: same.
  491. void CCassetteDoc::OnFontsSongs()
  492. {
  493. ASSERT_VALID (this);
  494. if (IDOK == ChangeFont (&m_lfontSongs))
  495.     {
  496.     SetModifiedFlag (TRUE);
  497.     UpdateAllViews (NULL, FONT_SONGS_CHANGE, NULL);
  498.     }
  499. ASSERT_VALID (this);
  500. }
  501.  
  502. //DoFontCommand handler.
  503. // This function is called by the view class when the user double-clicks 
  504. // on an area of the screen.  
  505. //PreConditions: valid object.
  506. //PostConditions: same.
  507. void CCassetteDoc::DoFontCommand (int WhichFont)
  508. {
  509. ASSERT_VALID (this);
  510. switch (WhichFont)
  511.     {
  512.     case FONT_ALBUM_CHANGE:
  513.         OnFontsAlbum ();
  514.         break;
  515.     case FONT_ARTIST_CHANGE:
  516.         OnFontsArtist ();
  517.         break;
  518.     case FONT_SONGS_CHANGE:
  519.         OnFontsSongs ();
  520.         break;
  521.     case FONT_NOTES_CHANGE:
  522.         OnFontsNotes ();
  523.         break;
  524.     default:
  525.         TRACE0 ("Danger Will Robinson, bad value passed to DoFontCommand ()\n");
  526.         ASSERT (FALSE);
  527.         break;
  528.     }
  529. ASSERT_VALID (this);
  530. }
  531.